﻿Imports System.Data.SqlClient
Public Class clsRolesDescuentos
    Private mintIdServicio As Integer
    Private mstrNombre As String
    Private mstrOrigen As String
    Private mstrDestino As String
    Private mstrHoraInicio As String
    Private mstrHoraTermino As String
    Private mintTipoDescuento As Integer
    Private mintMarcas As Integer
    Private mintPorcentajeMarcas As Integer
    Private mintPorcentajeDescuento As Integer
    Private mstrTipoDia As String
    Public Property IdServicio() As Integer
        Get
            Return mintIdServicio
        End Get
        Set(ByVal value As Integer)
            mintIdServicio = value
        End Set
    End Property
    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(ByVal value As String)
            mstrNombre = value
        End Set
    End Property

    Public Property Origen() As String
        Get
            Return mstrOrigen
        End Get
        Set(ByVal value As String)
            mstrOrigen = value
        End Set
    End Property
    Public Property Destino() As String
        Get
            Return mstrDestino
        End Get
        Set(ByVal value As String)
            mstrDestino = value
        End Set
    End Property
    Public Property HoraInicio() As String
        Get
            Return mstrHoraInicio
        End Get
        Set(ByVal value As String)
            mstrHoraInicio = value
        End Set
    End Property
    Public Property HoraTermino() As String
        Get
            Return mstrHoraTermino
        End Get
        Set(ByVal value As String)
            mstrHoraTermino = value
        End Set
    End Property
    Public Property TipoDescuento() As Integer
        Get
            Return mintTipoDescuento
        End Get
        Set(ByVal value As Integer)
            mintTipoDescuento = value
        End Set
    End Property
    Public Property Marcas() As Integer
        Get
            Return mintMarcas
        End Get
        Set(ByVal value As Integer)
            mintMarcas = value
        End Set
    End Property
    Public Property PorcentajeMarcas() As Integer
        Get
            Return mintPorcentajeMarcas
        End Get
        Set(ByVal value As Integer)
            mintPorcentajeMarcas = value
        End Set
    End Property
    Public Property PorcentajeDescuento() As String
        Get
            Return mintPorcentajeDescuento
        End Get
        Set(ByVal value As String)
            mintPorcentajeDescuento = value
        End Set
    End Property
    Public Property TipoDia() As String
        Get
            Return mstrTipoDia
        End Get
        Set(ByVal value As String)
            mstrTipoDia = value
        End Set
    End Property

    Public Sub New()
        mintIdServicio = 0
        mstrNombre = ""
        mstrOrigen = ""
        mstrDestino = ""
        mstrHoraInicio = ""
        mstrHoraTermino = ""
        mintTipoDescuento = 0
        mintMarcas = 0
        mintPorcentajeMarcas = 0
        mintPorcentajeDescuento = 0
        mstrTipoDia = ""

    End Sub
    Public Sub Buscar()
        Dim cnConn As New sqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From RolesDescuentos " & _
        " WHERE IdServicio=" & IdServicio

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mstrNombre = rdBuscar("Nombre")
            mstrOrigen = rdBuscar("Origen")
            mstrDestino = rdBuscar("Destino")
            mstrHoraInicio = rdBuscar("HoraInicio")
            mstrHoraTermino = rdBuscar("HoraTermino")
            mintTipoDescuento = rdBuscar("TipoDescuento")
            mintMarcas = rdBuscar("Marcas")
            mintPorcentajeMarcas = rdBuscar("PorcentajeMarcas")
            mintPorcentajeDescuento = rdBuscar("PorcentajeDescuento")
            mstrTipoDia = rdBuscar("TipoDia")
        Else
            mstrNombre = ""
            mstrOrigen = ""
            mstrDestino = ""
            mstrHoraInicio = ""
            mstrHoraTermino = ""
            mintTipoDescuento = 0
            mintMarcas = 0
            mintPorcentajeMarcas = 0
            mintPorcentajeDescuento = 0
            mstrTipoDia = ""
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into RolesDescuentos (" & _
                            "Nombre," & _
                            "Origen," & _
                            "Destino," & _
                            "HoraInicio," & _
                            "HoraTermino," & _
                            "TipoDescuento," & _
                            "Marcas," & _
                            "PorcentajeMarcas," & _
                            "PorcentajeDescuento," & _
                            "TipoDia)" & _
                            "VALUES (" & _
                            "substring(@Nombre,1,200)," & _
                            "substring(@Origen,1,100)," & _
                            "substring(@Destino,1,100)," & _
                            "substring(@HoraInicio,1,100)," & _
                            "substring(@HoraTermino,1,100)," & _
                            "@TipoDescuento," & _
                            "@Marcas," & _
                            "@PorcentajeMarcas," & _
                            "@PorcentajeDescuento," & _
                            "substring(@TipoDia,1,100))"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Origen", mstrOrigen)
        Command.Parameters.AddWithValue("Destino", mstrDestino)
        Command.Parameters.AddWithValue("HoraInicio", mstrHoraInicio)
        Command.Parameters.AddWithValue("HoraTermino", mstrHoraTermino)
        Command.Parameters.AddWithValue("TipoDescuento", mintTipoDescuento)
        Command.Parameters.AddWithValue("Marcas", mintMarcas)
        Command.Parameters.AddWithValue("PorcentajeMarcas", mintPorcentajeMarcas)
        Command.Parameters.AddWithValue("PorcentajeDescuento", mintPorcentajeDescuento)
        Command.Parameters.AddWithValue("TipoDia", mstrTipoDia)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro de roles", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update RolesDescuentos Set " & _
                            "Nombre=substring(@Nombre,1,50)," & _
                            "Origen=substring(@Origen,1,50)," & _
                            "Destino=substring(@Destino,1,50)," & _
                            "HoraInicio=substring(@HoraInicio,1,50)," & _
                            "HoraTermino=substring(@HoraTermino,1,50)," & _
                            "TipoDescuento=@TipoDescuento," & _
                            "Marcas=@Marcas," & _
                            "PorcentajeMarcas=@PorcentajeMarcas," & _
                            "PorcentajeDescuento=@PorcentajeDescuento," & _
                            "TipoDia=@TipoDia " & _
                            " Where IdServicio=@IdServicio"


        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdServicio", mintIdServicio)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Origen", mstrOrigen)
        Command.Parameters.AddWithValue("Destino", mstrDestino)
        Command.Parameters.AddWithValue("HoraInicio", mstrHoraInicio)
        Command.Parameters.AddWithValue("HoraTermino", mstrHoraTermino)
        Command.Parameters.AddWithValue("TipoDescuento", mintTipoDescuento)
        Command.Parameters.AddWithValue("Marcas", mintMarcas)
        Command.Parameters.AddWithValue("PorcentajeMarcas", mintPorcentajeMarcas)
        Command.Parameters.AddWithValue("PorcentajeDescuento", mintPorcentajeDescuento)
        Command.Parameters.AddWithValue("TipoDia", mstrTipoDia)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el registro de roles", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub

End Class
